//
//  MOAddressBookPeopleViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/10.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAddressBookPeopleViewController.h"
#import "MOAddressBookPeopleViewCell.h"
#import "MOAddressBookDetailViewController.h"

@interface MOAddressBookPeopleViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;


@end

@implementation MOAddressBookPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
}

-(void)creatView
{
    self.title = @"部门人员";
    
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.tableView];
}

#pragma mark -- 数据
-(void)initData
{
    self.dataSource = [NSMutableArray array];
//    NSArray *arr = @[@"掌心里",@"张起",@"张杰",@"王万",@"历久",@"民心",@"打幌",@"厉害",@"丽丽",@"哦莫"];
//    for (int i= 0 ; i<arr.count; i++) {
//        MOAddressBookPeopleModel *model = [[MOAddressBookPeopleModel alloc]init];
//        model.nameStr = arr[i];
//        model.sectionStr = @"局长级别的";
//        [self.dataSource addObject:model];
//    }
//    [self.tableView reloadData];
    
    
    NSDictionary *user = @{
                           @"groupid":self.orginId,
                           };
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ADDRESSBOOK_GETABBYBYORGID_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [[ADShowMessage shareShowMessage] showHUDAddedTo:self.view animated:YES];
    
    [PPNetworkHelper POST:postUrl parameters:user success:^(id responseObject) {
        NSLog(@"部门人员:%@",responseObject);
        
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        NSArray *arr = responseObject;
        for (int i= 0 ; i<arr.count; i++) {
            MOAddressBookPeopleModel *model = [[MOAddressBookPeopleModel alloc]init];
            model.nameStr = [NSString passNullForString:arr[i][@"truename"]];
            model.sectionStr = [NSString passNullForString:arr[i][@"depment"]];
            model.userId = [NSString passNullForString:arr[i][@"userid"]];
            model.room = [NSString passNullForString:arr[i][@"room"]];
            model.post = [NSString passNullForString:arr[i][@"post"]];
            model.tel = [NSString passNullForString:arr[i][@"tel"]];
            model.mobile = [NSString passNullForString:arr[i][@"mobile"]];
            model.ipphone = [NSString passNullForString:arr[i][@"ipphone"]];
            model.email = [NSString passNullForString:arr[i][@"email"]];
            model.ybcode = [NSString passNullForString:arr[i][@"ybcode"]];
            [self.dataSource addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:WINDOW];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookPeopleViewCell *cell = [MOAddressBookPeopleViewCell cellWithTableView:tableView andIndexPath:indexPath];
    
    MOAddressBookPeopleModel *model = self.dataSource[indexPath.row];
    
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(14) + kHConversions(3) + 17 + 13 + kHConversions(8) + kHConversions(15);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return nil;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookPeopleModel *model = self.dataSource[indexPath.row];
    MOAddressBookDetailViewController *vc = [[MOAddressBookDetailViewController alloc]init];
    model.orig_name = self.orginName;
    vc.addressModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, kWConversions(14) + kWConversions(17) + kWConversions(39), 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, kWConversions(14) + kWConversions(17) + kWConversions(39), 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, kWConversions(14) + kWConversions(17) + kWConversions(39), 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, kWConversions(14) + kWConversions(17) + kWConversions(39), 0, 0)];
    }
}



#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KGrayColor_F3F3F3;
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}



@end
